//
//  LBLinearLineTextureFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 4/2/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#include "LBLinearLineTextureFilter.h"
#include "LBGraphics.h"

char fShaderStrLinearLineTexture[] = ""
"precision mediump float;"

"varying vec2 vtexCoor;"
"varying vec4 vposition;"
"uniform sampler2D texture;"
"uniform sampler2D texture2;"
"uniform float opacity;"


// linear light function
"vec4 linearLight(vec4 base, vec4 blend, float opacity){"
"   vec4 result = base;"
"   if (blend.r > 0.5) result.r = base.r + 2.0*(blend.r-0.5);"
"   else result.r = base.r + 2.0*blend.r - 1.0;"
"   if (blend.g > 0.5) result.g = base.g + 2.0*(blend.g-0.5);"
"   else result.g = base.r + 2.0*blend.r - 1.0;"
"   if (blend.r > 0.5) result.b = base.r + 2.0*(blend.r-0.5);"
"   else result.b = base.r + 2.0*blend.r - 1.0;"
"   result = mix(base, result, opacity);"
"   result = clamp(result, 0.0, 1.0);"
"   return result;"
"}"
// END linear light function


"void main()"
"{"
"    lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"    lowp vec4 blendColor = texture2D(texture2, vtexCoor);"
"    textureColor = linearLight(textureColor, blendColor, opacity);"
"    gl_FragColor = textureColor;"
"}";

LBLinearLineTextureFilter::LBLinearLineTextureFilter(){
    init(0, fShaderStrLinearLineTexture);
    LBGraphics::getInstance()->useProgram(program);
    opacityUniform = program->uniformIndex("opacity");
    setOpactiy(1.0f);
}